<?php

class Gallery_Backend_Categories_Edit extends Kernel_Action_Form_Simple 
{
    
    /**
     * Path to the file template.
     *
     * @var string
     */
    protected $_template = 'gallery/backend/categories/container.tpl';
    
    /**
     * Основная таблица, в которой хранится редактируемый элемент.
     *
     * @var string
     */
    protected $_table = 'photo_categories';
    
    /**
     * Initialize form elements. 
     *
     */
    protected function _initElements() 
    {
        parent::_initElements();
        
        $this->_setElementParam('parent_id', array(
            'caption' => __('PARENT_CATEGORY'), 
               'type' => 'list',
             'values' => array(0 => __('NULL')) + Gallery_Backend_Utils::parents(), 
              'value' => Gallery_Backend_Utils::parent(), 
        ));
        $this->_setAsRequired('parent_id');
        $this->_setElementParam('url', array
        (
            'filters' => array
            (
                'Gallery_Form_Element_Filter_AutoUrl', 
                'Gallery_Form_Element_Filter_RemoveExtension', 
                'transliterate', 
                'slug', 
                'Gallery_Form_Element_Filter_AddExtension'
            ), 
            'validators' => array
            (
                array('Kernel_Validator_Db_NoRecordExists', array
                    (
                    'table' => 'photo_categories', 
                    'field' => 'url', 
                    'exclude' => array('id' => $this->id), 
                    'message' => 'SUCH_URL_ALREADY_EXISTS', 
                    )
                )
            )
        ));
        $this->_setElementParam('position', 'type', 'position');
        
        $this->_setAsRequired();
        
        $this->_addElement('id', 'hidden', array('value' => $this->id));
        
        // delete button
        if ('Gallery_Backend_Categories_Edit' == get_class($this)) {
            Application::getResponse()->assign(array('buttons' => array(
                'delete' => Backend_Filter_Structure::get() + array(
                    'action' => 'delete', 'id' => $this->id), 
                )));
        }
    }
    
    /**
     * Add new record.
     *
     */
    protected function _insert() 
    {
        $data = $this->_data();
        
        $this->id = Kernel::factory('Kernel_Form', 'Order', $this->_params())
            ->addNode($this->_getValue('position'), 
                array_extract($this->_data(), array('name', 'url')));
    }
    
    /**
     * Update current record.
     *
     */
    protected function _update() 
    {
        Kernel::factory('Kernel_Form', 'Order', $this->_params())
            ->moveNode($this->id, $this->_getValue('position'));
        
        database::update($this->_table, array_extract($this->_data(), array('name', 'url')), array($this->_id_field => $this->id));
    }
    
    /**
    * Return params for current node type
    * 
    * @return array
    */
    protected function _params() 
    {
        $data = $this->_data();
        
        return array(
            'table' => $this->_table, 
            'parent' => $data['parent_id'] ? $data['parent_id'] : null, 
            );
    }
    
    protected function _done() 
    {
        $this->redirect_url .= '&' . Gallery_Backend_Utils::FILTER . '=' . $this->id;
        parent::_done();
    }
    
}
